1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.
event.*;
4 import java.util.regex.*;

5
6 class
UpdateFrame extends JFrame
7 {
8     Container c;
9     JLabel lblRno , lblName;
10     JTextField txtRno, txtName;
11     JButton btnSave , btnBack;
12     JPanel p1,p2;
13
14     UpdateFrame()
15     {
16         c = getContentPane();
17         c.setLayout(
new BoxLayout(c , BoxLayout.Y_AXIS));
18         c.setBackground(Color.orange);
19
20         p1 =
new JPanel();
21         lblRno =
new JLabel("Rno: ");
22         txtRno =
new JTextField((4));
23         lblName =
new JLabel("Name: ");
24         txtName =
new JTextField((10));
25         p1.
add(lblRno);
26         p1.
add(txtRno);
27         p1.
add(lblName);
28         p1.
add(txtName);
29         p1.setBackground(Color.orange);
30         c.
add(p1);
31
32         p2 =
new JPanel();
33         btnSave =
new JButton("Save");
34         btnSave.setBackground(Color.orange);
35         btnBack =
new JButton("Back");
36         btnBack.setBackground(Color.orange);
37         p2.
add(btnSave);
38         p2.
add(btnBack);
39         c.
add(p2);
40
41         setTitle(
"Update Student Info.");
42         setLocationRelativeTo(
null);
43     setSize(
500,400);
44         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
45         setVisible(
true);
46
47
48         btnBack.addActionListener(
new ActionListener(){
49             
public void actionPerformed(ActionEvent ae){
50             MainFrame a =
new MainFrame();
51             dispose();
52             }
53         });
54
55         btnSave.addActionListener(
new ActionListener(){
56             
public void actionPerformed(ActionEvent ae){
57                 
try{
58                 String rno = txtRno.getText();
59                 String name = txtName.getText();
60                 DbHandler db =
new DbHandler();
61                 
if( txtName.getText().isEmpty())
62                 {
63                     
throw new InvalidFieldException();
64                 }
65                 
if(Integer.parseInt(rno)<0)
66                 {
67                     
throw new NumberGreaterException();
68                 }
69                 
if(Pattern.matches("[a-zA-Z]+", name) == false){
70                     
throw new Exception();
71                 }
72                 
73                 db.UpdateStudent(Integer.parseInt(rno),name);
74                 }
75                 
catch(NumberFormatException nfe)
76                 {
77                     JOptionPane.showMessageDialog(
new JDialog(), "Please Enter Integer");
78                 }
79                 
catch(NumberGreaterException nge)
80                 {
81                     JOptionPane.showMessageDialog(
new JDialog(), "Number should be greater than 0");
82                 }
83                 
catch(InvalidFieldException iae)
84                 {
85                     JOptionPane.showMessageDialog(
new JDialog(), "Field(s) should not be blank");
86                 }
87                 
catch(Exception e)
88                 {
89                     JOptionPane.showMessageDialog(
new JDialog(), "Name should not contain Integers");
90                 }
91
92             }
93         });
94
95     }
96
97     
public static void main(String[] args)
98     {
99         UpdateFrame uf =
new UpdateFrame();
100 }
101
102 }


Gõ tìm kiếm nhanh...